<?php
/**
* @package mosRest
* @author Chad Auld and Ozgur Cem Sen (code@brilaps.com)
* @copyright Brilaps, LLC (http://brilaps.com)
* @link http://brilaps.com || http://wiki.brilaps.com
* @license http://www.opensource.org/licenses/gpl-license.php GNU/GPL v.2.
*/

/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

//unfortunately!
if (phpversion() < 5) {
    die( 'REST API requires PHP5 or later.' );
}

//Fix to allow compatibility with J!
if (!function_exists( 'T_' )) { function T_($string) {return $string;} }

//Bring in the base REST class
global $mosConfig_absolute_path;
$base_class = $mosConfig_absolute_path.'/components/com_rest/rest.class.php';
if (file_exists($base_class)) {
    require_once($base_class);
} else {
    die(T_('Before utilizing com_rest_stats the com_rest base component must first be installed!'));
}

require_once($mainframe->getPath('class'));

switch( $task ) {
	case "dataRetrival":
	dataRetrival( $option );
	break;
	
	default:
	invalidRequest( $option );
	break;
}

/**
* Handles unknown task requests and responds with error wrapped in the requested encoding format
*/
function invalidRequest() {
    if (class_exists('mRestStats')) {
        $mRest = new mRestStats();
    } else {
        die(T_('Unable to load class: mRestStats'));
    }

    //Verify return format
	$returnFormat = trim(mosGetParam($_GET, 'output', ''));
	if ($mRest->validateReturnFormat($returnFormat)===false) {
	    $returnFormat='json'; //force json
	}
	$htmlReturnStatusCode = 400;
	$errorMessage = T_('Invalid request');
	$data = $mRest->restError($errorMessage, $returnFormat);
	$mRest->restResponse($htmlReturnStatusCode, $returnFormat, $data);
}

/**
* Performs the actual data retrival as request by the external REST interface
* See the wiki for parameter explanations - http://wiki.brilaps.com/wikka.php?wakka=MOStlyRESTcomStats
*/
function dataRetrival() {
	global $mosConfig_absolute_path;
	
	if (class_exists('mRestStats')) {
        $mRest = new mRestStats();
    } else {
        die(T_('Unable to load class: mRestStats'));
    }
    
	//Setup main variables
	$appId = trim(mosGetParam($_GET, 'appid', ''));	
	$limit = intval(mosGetParam($_GET, 'limit', 0));
	$callback = trim(mosGetParam($_GET, 'callback', ''));
	$requestType = trim(mosGetParam($_GET, 'type', ''));
	$validRequestTypes = array('whosonline', 'agent', 'content', 'search');
	
	//Verify return format
	$returnFormat = trim(mosGetParam($_GET, 'output', ''));
	if ($mRest->validateReturnFormat($returnFormat)===false) {
	    $returnFormat='json'; //force json
	}
	
	//Validate we have a properly formed request
	$htmlReturnStatusCode = 200; //Start as OK
	$uri = $mRest->full_url();
	if ($mRest->verifyRestAPIKey($appId, $requestType, $uri)===false) {
		$htmlReturnStatusCode = 401;
		$errorMessage = T_('Invalid or blocked appid used in request!');
		$data = $mRest->restError($errorMessage, $returnFormat);
	} else if (!in_array($requestType, $validRequestTypes)) {
		$htmlReturnStatusCode = 400;
		$errorMessage = T_('Invaid request type!');
		$data = $mRest->restError($errorMessage, $returnFormat);
	} else if ($returnFormat=='json' && !empty($callback) && $mRest->verifyJSONCallbackName($callback)===false) {
		$htmlReturnStatusCode = 400;
		$errorMessage = T_('Invalid JSON callback parameter!');
		$data = $mRest->restError($errorMessage, $returnFormat);
	} 
	
	//Continue with data retrival if the request parameters are valid
	if ($htmlReturnStatusCode==200) {
		switch ($requestType) {
			case 'content':
				$data = $mRest->getContentStats($returnFormat, $limit, $callback);
			break;
			
			case 'agent':
				$data = $mRest->getAgentStats($returnFormat, $callback);
			break;
			
			case 'whosonline':
				$data = $mRest->getWhosOnline($returnFormat, $callback);
			break;
			
			case 'search':
				$data = $mRest->getSearchStats($returnFormat, $limit, $callback);
			break;
			
			default:
			break;
		}
	}
	
	//Do final response
	$mRest->restResponse($htmlReturnStatusCode, $returnFormat, $data);
}

?>